package pt.utl.ist.project_ccu.android_libs;

import java.util.ArrayList;
import java.util.List;

import pt.utl.ist.project_ccu.android_libs.exceptions.TagNameAlreadyExistsExcepction;
import android.app.Activity;
import android.graphics.Point;
import android.view.Display;

public class SavedData {
	
	private static SavedData _instance = null;
	private TagManager _tagManager = null;
	private PhotoManager _photoManager = null;
	private AlbumManager _albumManager = null;
	private int _screenWidth;
	private int _screenHeight;
	
	public static SavedData getInstance() {
		return _instance;
	}
	
	public static void initInstance(Activity activity) {
		if(_instance == null) {
			_instance = new SavedData(activity);
		}
	}
	
	private SavedData(Activity activity) {
		Display display = activity.getWindowManager().getDefaultDisplay();
		_screenWidth = display.getWidth();
		_screenHeight = display.getHeight();
		_tagManager = new TagManager(activity);
		_photoManager = new PhotoManager(activity);
		_albumManager = new AlbumManager(activity);
	}
	
	public int[] getScreenDimensions() {
		return new int[] {_screenWidth, _screenHeight };
	}
	
	public void updateActivity(Activity activity) {
		_tagManager.updateActivity(activity);
		_photoManager.updateActivity(activity);
		_albumManager.updateActivity(activity);
	}
	
	public void addPhoto(Photo photo) {
		_photoManager.addPhoto(photo);
		_photoManager.writeToDisk();
	}
	
	public void addTag(String tagType, String tagName)   {
		_tagManager.addTag(new Tag(tagType,tagName));
		_tagManager.writeToDisk();
	}

	
	
	public ArrayList<Photo> getAllPhotos() {
		return _photoManager.getAllPhotos();
	}
	
	public List<Photo> getUntaggedPhotos() {
		return _photoManager.getUntaggedPhotos();
	}
	
	public List<String> getAllTags() {
		return _tagManager.getExistentTags();	
	}
	
	public void addNewTypeOfTag(String tagType) {
		_tagManager.createNewTypeOfTag(tagType);
	}
	
	public void clearData() {
		_tagManager.clearTags();
		_photoManager.clearPhotos();
	}
	
	public void removePhoto(Photo photo) {
		_albumManager.removePhoto(photo);
		_photoManager.removePhoto(photo);
		_photoManager.writeToDisk();
		_albumManager.writeToDisk();
	}
	
	public void removeAlbum(Album al) {
		_albumManager.removeAlbum(al);
		_photoManager.removePhotos(al.getPhotos());
		_photoManager.writeToDisk();
		_albumManager.writeToDisk();
	}
	
	public List<Album> getAlbuns() {
		return _albumManager.getAlbuns();
	}
	
	public void addAlbum(Album al) {
		_albumManager.addAlbum(al);
		_albumManager.writeToDisk();
	}
	
	public void createAlbum(String albumName, List<Tag> tags) {
		_albumManager.createAlbum(albumName, tags);
		_albumManager.writeToDisk();
	}
	
	public Album getAlbumByName(String albumName) {
		Album album = _albumManager.getAlbumByName(albumName);
		if(album != null) {
			album.updatePhotos(_photoManager.getAllPhotos());
		}
		return album;
	}
	
	public ArrayList<String> getTagNames(String tagType) {
		return _tagManager.getTagNames(tagType);
	}
	
	public ArrayList<Photo> getPhotosByTags(ArrayList<Tag> tags) {
		return _photoManager.getPhotoByTags(tags);
	}
	
	public void clearPhotos() {
		_photoManager.clearPhotos();
	}

	public Photo getPhoto(Photo photo) {
		return _photoManager.getPhoto(photo);
		
	}

}
